﻿(function () {
    $(function () {

        var _$brandsTable = $('#BrandsTable');
        var _brandService = abp.services.app.brand;

        //权限
        var _permissions = {
            create: abp.auth.hasPermission('Pages.Brand.Create'),
            edit: abp.auth.hasPermission('Pages.Brand.Edit'),
            'delete': abp.auth.hasPermission('Pages.Brand.Delete')
        };

        var _createModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Brand/CreateModal',//加载视图
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Brand/_CreateModal.js',//加载对应js
            modalClass: 'CreateBrandModal'
        });

        var _editModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Brand/EditModal',
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Brand/_EditModal.js',
            modalClass: 'EditBrandModal'
        });


        _$brandsTable.jtable({
            title: app.localize('BrandManager'),//标题
            paging: true,//启用分页
            sorting: true,//启用排序
            multiSorting: true,//启用多列排序
            actions: {
                listAction: {
                    method: _brandService.getBrands//获取列表方法
                }
            },
            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),//操作列
                    width: '15%',
                    sorting: false,
                    display: function (data) {
                        var $span = $('<span></span>');
                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    _editModal.open({ id: data.record.id });
                                });
                        }
                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    deleteBrand(data.record);
                                });
                        }
                        return $span;
                    }
                },
                name: {
                    title: app.localize('Brand_Name'),
                    width: '20%'
                }
            }
        });
        //获取列表
        function getBrands(reload) {
            if (reload) {
                _$brandsTable.jtable('reload');
            } else {
                _$brandsTable.jtable('load', {
                    filter: $('#BrandsTableFilter').val()
                });
            }
        }

        //删除分类
        function deleteBrand(brand) {
            abp.message.confirm(
                app.localize('BrandDeleteWarningMessage', brand.name),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _brandService.deleteBrand({
                            id: brand.id
                        }).done(function () {
                            getBrands();
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }

        //页面加载完执行
        getBrands();

        //添加点击事件
        $('#CreateNewBrandButton').click(function () {
            _createModal.open();
        });

        //搜索点击事件
        $('#GetBrandsButton').click(function (e) {
            //取消事件的默认动作
            e.preventDefault();
            getBrands();
        });

        //事件注册
        abp.event.on('app.createBrandModalSaved', function () {
            getBrands(true);
        });
        abp.event.on('app.editBrandModalSaved', function () {
            getBrands(true);
        });
    });
})();